package com.best.smart.logfile;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class LogActivity extends FragmentActivity {


    //int[] iconArr = {R.drawable.};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
//        CheckBox cb_log = findViewById(R.id.cb_log);
//        cb_log.setChecked(VPNLog.wtf);
//        cb_log.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                VPNLog.wtf = b;
//                SharedPreferences sp = getApplicationContext().getSharedPreferences("log", Context.MODE_PRIVATE);
//                sp.edit().putBoolean("wtf", b).commit();
//                restartApp(LogActivity.this);
//            }
//        });
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        List<String> list = getLogFiles();
        LogAdapter adapter = new LogAdapter(this, list);
        recyclerView.setAdapter(adapter);
    }


    private List<String> getLogFiles() {
        List<String> logFileList = new ArrayList<>();
        File filesDir = getFilesDir();
        File[] files = filesDir.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".log")) {
                    logFileList.add(file.getAbsolutePath());
                }
            }
        }
        return logFileList;
    }


    public static void restartApp(Context context) {
//        Intent intent = context.getPackageManager()
//                .getLaunchIntentForPackage(context.getPackageName());
//        if (intent != null) {
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        }
//
//        PendingIntent pendingIntent = PendingIntent.getActivity(
//                context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//
//        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//        if (alarmManager != null) {
//            alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);
//        }

        System.exit(0);
    }

}
